<template>
  <el-card>
    <div class="welcome">账号注册</div>
    <el-form ref="registerForm" :model="registerForm" :rules="formRule">
      <el-form-item label="手机号码" prop="accountNumber" >
        <el-input v-model.trim="registerForm.accountNumber" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-row type="flex" justify="space-between">
        <el-col :span="15">
          <el-form-item label="验证码" prop="verification">
            <el-input v-model.trim="registerForm.verification" placeholder="请输入验证码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-button
            :class="{'disabled':isDisable}"
            @click="getVerification"
            type="primary"
            :disabled="isDisable"
          >{{VerificationInfo}}</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-button @click="formValidate('registerForm',registerAccount)">注册并登陆</el-button>
    <div class="link-box">
      <router-link to="/login">返回</router-link>
    </div>
  </el-card>
</template>
<script>
import mixin from "@/utils/mixin.js";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      registerForm: {
        accountNumber: "",
        verification: "",
      },
      formRule: {
        accountNumber: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        verification: [
          { required: true, message: "请输入验证码", trigger: "blur" },
        ],
      },
      ref: "registerForm",
    };
  },
  mixins: [mixin],
  methods: {
    ...mapMutations(["setUserId", "setUserName", "setPhoneNumber"]),
    // 获取验证码
    getVerification() {
      if (!/^1[3456789]\d{9}$/.test(this.registerForm.accountNumber))
        return this.$message.error("请输入正确的手机号码");
      this.isDisable = true;
      this.$http(
        `/trademark/front/message/appointMsg/${this.registerForm.accountNumber}`
      ).then(
        ({ data: res }) => {
          if (res.code !== 200) {
            this.isDisable = false;
            return ;
          }
          this.$message.success("验证码发送成功");
          this.VerificationTime();
        },
        (err) => {
          this.isDisable = false;
//          this.$message.error("网络错误");
        }
      );
    },
    // 注册并登陆
    registerAccount() {
      sessionStorage.removeItem("token");
      this.$http
        .post("/trademark/front/register", {
          phoneNum: this.registerForm.accountNumber,
          inputCode: this.registerForm.verification,
        })
        .then(
          ({ data: res }) => {
            if (res.code !== 200) return ;
            this.$message.success("登录成功");
            sessionStorage.setItem("token", JSON.stringify(res.data.token));
            this.setUserId(res.data.id);
            this.setUserName(res.data.username);
            this.setPhoneNumber(this.registerForm.accountNumber);
            this.$router.push("/");
          },
//          (err) => this.$message.error("网络错误")
        );
    },
  },
  created() {
    document.addEventListener("keydown", this.keydown);
  },
  beforeDestroy() {
    document.removeEventListener("keydown", this.keydown);
  },
};
</script>
<style lang="less" scoped>
.el-card {
  width: 25.5rem;
  margin: 0 auto;
  margin-top: 2.5rem;
  padding: 0.5rem;
  color: #333333;
  border-radius: 0.4rem;
  .welcome {
    font-size: 2em;
    font-weight: bold;
    line-height: 2em;
    text-align: center;
  }
  .el-form {
    margin-top: 1rem;
    /deep/.el-form-item__label {
      line-height: 1rem;
    }
    /deep/.el-input__inner {
      height: 2.5rem;
    }
    .el-button {
      margin-top: 26px;
      font-size: 1em;
      border: none;
    }
    .disabled {
      background-color: #999;
    }
  }

  /deep/ .el-form-item__label{
     margin-bottom: 6px;
  }

  .el-button {
    width: 100%;
    height: 2.5rem;
    background-color: #013d87;
    color: #fff;
    margin-top: 2rem;
    font-size: 1.2em;
  }
  .link-box {
    text-align: center;
    padding-top: 1em;

  }
  a {
    color: #666666;
  }
  .change-button {
    color: #666666;
    cursor: pointer;
  }
}
</style>